package com.sikhyeon.datastructure.controller;

import com.sikhyeon.datastructure.model.LinkedList;
import javafx.animation.FadeTransition;
import javafx.animation.TranslateTransition;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Line;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import javafx.util.Duration;

import java.io.IOException;

public class LinkedListController {
    @FXML
    private Pane linkedListContainer;

    @FXML
    private StackPane messagePane;

    @FXML
    private Label messageLabel;

    private LinkedList linkedList = new LinkedList();
    private int valueCounter = 1;

    public void goBack(ActionEvent event) throws IOException {
        FXMLLoader loader = new FXMLLoader(getClass().getResource("/com/sikhyeon/datastructure/view/MainView.fxml"));
        Parent root = loader.load();
        Stage stage = (Stage) ((Node) event.getSource()).getScene().getWindow();
        Scene scene = new Scene(root, 800, 600); // 창 크기를 고정
        stage.setScene(scene);
        stage.show();
    }

    public void addValue(ActionEvent event) {
        int value = valueCounter++;
        linkedList.add(value);
        updateLinkedListView();
    }

    private void addLinkedListElement(int value) {
        LinkedList.Node current = linkedList.getHead();
        double x = 50;
        double y = 50;
        double nodeWidth = 100;
        double nodeHeight = 50;
        double nodeSpacing = 20;
        int nodeCount = 0;

        while (current.next != null) {
            nodeCount++;
            x += nodeWidth + nodeSpacing;
            if (nodeCount % 4 == 0) { // 화면을 넘어가면 줄바꿈
                x = 50;
                y += 100;
            }
            current = current.next;
        }

        Rectangle valueRect = new Rectangle(50, 50, 50, 50);
        valueRect.setFill(Color.LIGHTBLUE);
        Text valueText = new Text(String.valueOf(current.value));
        valueText.setX(65);
        valueText.setY(130);

        Rectangle addressRect = new Rectangle(100 ,  50,  25  , 50);
        addressRect.setFill(Color.LIGHTGREEN);
        Text addressText = new Text("NULL");
        addressText.setX(112);
        addressText.setY(130);

        Group nodeGroup = new Group(valueRect, addressRect, valueText, addressText);
        nodeGroup.setLayoutX(x - nodeWidth - nodeSpacing); // 시작 위치를 화면 밖으로 설정
        nodeGroup.setLayoutY(y);

        linkedListContainer.getChildren().add(nodeGroup);

        TranslateTransition transition = new TranslateTransition(Duration.seconds(0.5), nodeGroup);
        transition.setFromX(x - nodeWidth - nodeSpacing);
        transition.setToX(x);
        transition.play();

        if (linkedList.getHead() != null && nodeCount % 4 != 3) {
            Line line = new Line(x - nodeWidth - nodeSpacing + 100, y + 25, x + 50, y + 25);
            line.setOpacity(0);
            linkedListContainer.getChildren().add(line);

            FadeTransition lineTransition = new FadeTransition(Duration.seconds(0.5), line);
            lineTransition.setFromValue(0);
            lineTransition.setToValue(1);
            lineTransition.play();
        }

        updateLinkedListView();
    }

    public void removeValue(ActionEvent event) {
        linkedList.remove();
        updateLinkedListView();
    }

    private void updateLinkedListView() {
        linkedListContainer.getChildren().clear();
        LinkedList.Node current = linkedList.getHead();
        double x = 50;
        double y = 50;
        double nodeWidth = 100;
        double nodeHeight = 50;
        double nodeSpacing = 20;
        int nodeCount = 0;

        while (current != null) {
            // 노드 값 부분
            Rectangle valueRect = new Rectangle(90, 50);
            valueRect.setFill(Color.LIGHTBLUE);
            Text valueText = new Text(String.valueOf(current.value));
            valueText.setX(25);
            valueText.setY(30);

            // 노드 주소 부분
            Rectangle addressRect = new Rectangle(65, 50);
            addressRect.setFill(Color.LIGHTGREEN);
            Text addressText = new Text(current.next == null ? "NULL" : "->");
            addressText.setX(65);
            addressText.setY(30);

            Group nodeGroup = new Group(valueRect, addressRect, valueText, addressText);
            nodeGroup.setLayoutX(x);
            nodeGroup.setLayoutY(y);

            linkedListContainer.getChildren().add(nodeGroup);

            if (current.next != null && nodeCount % 4 != 3) {
                Line line = new Line(x + 100, y + 25, x + nodeWidth + nodeSpacing, y + 25);
                linkedListContainer.getChildren().add(line);
            }

            nodeCount++;
            x += nodeWidth + nodeSpacing;

            // 4개 단위로 줄바꿈
            if (nodeCount % 4 == 0) {
                x = 50;
                y += 100;
            }

            current = current.next;
        }
    }

    public void resetLinkedList(ActionEvent event) {
        linkedList = new LinkedList();
        valueCounter = 1;
        linkedListContainer.getChildren().clear();
        showMessage("Linked List initialized");
    }

    private void showMessage(String message) {
        messageLabel.setText(message);
        FadeTransition fadeIn = new FadeTransition(Duration.seconds(0.5), messagePane);
        fadeIn.setFromValue(0);
        fadeIn.setToValue(1);
        fadeIn.play();

        FadeTransition fadeOut = new FadeTransition(Duration.seconds(0.5), messagePane);
        fadeOut.setFromValue(1);
        fadeOut.setToValue(0);
        fadeOut.setDelay(Duration.seconds(1.5));
        fadeOut.play();
    }
}
